package com.endoc.news.fragment.newslist;


import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.endoc.news.fragment.base.BaseHolder;
import com.endoc.news.fragment.base.BaseViewModel;
import com.endoc.news.fragment.base.IBaseCustomView;
import com.endoc.news.fragment.newslist.view.pictureview.PictureView;
import com.endoc.news.fragment.newslist.view.pictureview.PictureViewModel;
import com.endoc.news.fragment.newslist.view.titleview.TitleView;
import com.endoc.news.netWoke.bean.NewsListBean;
import com.orhanobut.logger.Logger;

import java.util.List;

public class NewsRecycleAdapter extends RecyclerView.Adapter<BaseHolder> {
    //List<NewsListBean.Contentlist> mContentlists;
    List<BaseViewModel> mBaseViewModels;
    int TYPE_HAVE_URL = 0;
    int TYPE_NO_URL = 1;
  /*  class MyHolder extends RecyclerView.ViewHolder{
         ImageView image;
         TextView itemTitle;
        public MyHolder(@NonNull View itemView,int viewType) {
            super(itemView);
            if(viewType==TYPE_HAVE_URL){
                image = (ImageView) itemView.findViewById(R.id.image);
                itemTitle = (TextView) itemView.findViewById(R.id.item_title);
            }else if(viewType==TYPE_NO_URL){
                itemTitle = (TextView) itemView.findViewById(R.id.item_title);
            }
        }
    }*/

  /*  @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==TYPE_HAVE_URL){
            PictureView pictureView = new PictureView(parent.getContext());
            //View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.picture_title_view, parent, false);
            //return new MyHolder(inflate,viewType);
            return new BaseHolder(pictureView);
        }else if(viewType==TYPE_NO_URL){
            //View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.title_view, parent, false);
            TitleView titleView = new TitleView(parent.getContext());
            return new MyHolder(titleView,viewType);
        }
        return null;
    }*/

/*    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, final int position) {
            holder.itemTitle.setText(mContentlists.get(position).title);
            if(holder.image!=null&&mContentlists.size()>0){//如果有url,那么加载
                Logger.d("imageurls==="+mContentlists.get(position).imageurls.get(0).url);
                Glide.with(holder.image.getContext())
                        .load(mContentlists.get(position).imageurls.get(0).url)
                        .transition(withCrossFade())
                        .into(holder.image);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Logger.d("跳转网页=="+mContentlists.get(position).link);
                }
            });
    }*/


    //Holder.getRootView就是这个条目
    @NonNull
    @Override
    public BaseHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HAVE_URL) {
            PictureView pictureView = new PictureView(parent.getContext());
            //View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.picture_title_view, parent, false);
            //return new MyHolder(inflate,viewType);
            return new BaseHolder((IBaseCustomView)pictureView);
        } else if (viewType == TYPE_NO_URL) {
            //View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.title_view, parent, false);
            TitleView titleView = new TitleView(parent.getContext());
            return new BaseHolder((IBaseCustomView)titleView);
        }
        return null;
    }

    //绑定的时候应该做什么
    @Override
    public void onBindViewHolder(@NonNull BaseHolder holder, int position) {
        holder.bindViewModel(mBaseViewModels.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        //if(mContentlists.get(position).imageurls!=null&mContentlists.get(position).imageurls.size()>0){
        if (mBaseViewModels != null && mBaseViewModels.get(position) instanceof PictureViewModel) {
//            Logger.d("getItemViewType");
            return TYPE_HAVE_URL;
        } else {
            return TYPE_NO_URL;
        }
    }

    @Override
    public int getItemCount() {
        if (mBaseViewModels != null && mBaseViewModels.size() != 0) {
            return mBaseViewModels.size();
        }
        return 0;
    }

    /*  public void setData(List<NewsListBean.Contentlist> contentlists){
          mContentlists =contentlists;
          notifyDataSetChanged();
      }*/
    public void setData(List<BaseViewModel> baseViewModels) {
        mBaseViewModels = baseViewModels;
        notifyDataSetChanged();
    }
}
